package com.xnx3.microsoft;

import com.jacob.activeX.ActiveXComponent;
import com.xnx3.Log;
import com.xnx3.bean.ActiveBean;
import com.xnx3.robot.Robot;
import com.xnx3.robot.support.CoordBean;
import demo.MhxyDemo;

import java.util.List;

/**
 * 模拟鼠标操作
 * @author 杨文睿
 *
 */
public class Mouse {
	private ActiveXComponent active=null;
	private Log log;
	private Robot robot;
	
	/**
	 * @param activeBean 传入操作的目标对象 {@link Com#getActiveXComponent()}
	 */
	public Mouse(ActiveBean activeBean) {
		this.active=activeBean.getDm();
		log=new Log();
		robot = new Robot();
		robot.setSourcePath(MhxyDemo.class);
	}
	
	/**
	 * 鼠标移动至某个坐标点点击鼠标
	 * @param x 点击的X坐标
	 * @param y 点击的Y坐标
	 * @param click <li>左键为true
	 * 				<li>右键为false
	 */
	public boolean mouseClick(int x,int y,boolean click){
		
		try {
			int move=active.invoke("MoveTo",x,y).getInt();
			new Sleep().sleep(300);	//避免卡屏
			
			int clickResult=0;
			if(click){
				clickResult=active.invoke("LeftClick").getInt();
			}else{
				clickResult=active.invoke("RightClick").getInt();
			}
			if(move==1&&clickResult==1){
				return true;
			}else{
				return false;
			}
			
		} catch (Exception e) {
			log.debug(this,"mouseClick","鼠标移动至某个坐标点点击鼠标异常捕获:"+e.getMessage());
			return false;
		}
	}
	
	/**
	 * 鼠标点击
	 * @param leftClick <li>左键点击传入true
	 * 					<li>右键点击传入false
	 */
	public void mouseClick(boolean leftClick){
		try {
			if(leftClick){
				active.invoke("LeftClick");
			}else{
				active.invoke("rightClick");
			}
		} catch (Exception e) {
			log.debug(this,"mouseClick","鼠标点击异常捕获:"+e.getMessage());
		}
	}
	
	/**
	 * 鼠标移动到指定点
	 */
	public void mouseMoveTo(int x,int y){
		try {
			active.invoke("MoveTo",x,y);
		} catch (Exception e) {
			log.debug(this,"mouseMoveTo","鼠标移动捕获异常:"+e.getMessage());
		}
	}

	/**
	 * 鼠标移动到指定点
	 */
	public void mouseMoveR(int x,int y){
		try {
			Thread.sleep(100);
			System.out.println("MoveR:"+x+","+y);
			active.invoke("MoveR",x,y);
			Thread.sleep(100);
		} catch (Exception e) {
			log.debug(this,"mouseMoveR","鼠标移动捕获异常:"+e.getMessage());
		}
	}
	
	/**
	 * 按住鼠标左键
	 */
	public void leftDown(){
		try {
			active.invoke("LeftDown");
		} catch (Exception e) {
			log.debug(this,"leftDown","鼠标左键按住捕获异常:"+e.getMessage());
		}
	}
	
	/**
	 * 弹起鼠标左键
	 */
	public void leftUp(){
		try {
			active.invoke("LeftUp");
		} catch (Exception e) {
			log.debug(this, "leftUp","鼠标左键弹起捕获异常:"+e.getMessage());
		}
	}
	
	/**
	 * 鼠标向下滚
	 */
	public void wheelDown(){
		try {
			active.invoke("WheelDown");
		} catch (Exception e) {
			log.debug(this, "wheelDown","鼠标向下滚异常:"+e.getMessage());
		}
	}
	
	/**
	 * 鼠标向上滚
	 */
	public void wheelUp(){
		try {
			active.invoke("WheelUp");
		} catch (Exception e) {
			log.debug(this, "wheelUp","鼠标向上滚异常:"+e.getMessage());
		}
	}

	/**
	 * 鼠标防漂移精准移动 梦幻西游客户端使用
	 */
	public boolean mouseRealMove(int x,int y,int xwrong,int ywrong,int[] reange){
		boolean flag = false;
		try {
			mouseMoveTo(x,y);
			CoordBean tmpcoord = new CoordBean();
			for(int m =0 ;m <5;m++){
				List<CoordBean> list = robot.imageSearch(reange[1], reange[2], reange[3],reange[4], "pic.bmp", Robot.SIM_ACCURATE);
				if(list.size() > 0 ){
					tmpcoord = list.get(0);
					break;
				}
			}

			for(int i = 0 ;i <20;i++){
				if ((Math.abs(tmpcoord.getX() - x) <= (xwrong+17) && Math.abs(tmpcoord.getY() - y) <= (ywrong +12)) ){
					flag = true;
					System.out.println(i);
					break;
				}else{
					mouseMoveR(x-tmpcoord.getX()+17,y-tmpcoord.getY()+12);

					for(int m =0 ;m <5;m++){
						List<CoordBean> list = robot.imageSearch(reange[1], reange[2], reange[3],reange[4], "pic.bmp", Robot.SIM_ACCURATE);
						if(list.size() > 0 ){
							tmpcoord = list.get(0);
							break;
						}
					}

				}
			}
		} catch (Exception e) {
			log.debug(this, "mouseRealMove","鼠标漂移移动异常:"+e.getMessage());
			return false;
		}
		return flag;
	}

	/**
	 * 鼠标左键点击
	 */
	public boolean mouseLeftClick(){
		try {
			int  i = active.invoke("LeftClick").getInt();
			if( i == 1){
				return true;
			}
		} catch (Exception e) {
			log.debug(this,"mouseLeftClick","鼠标点击异常捕获:"+e.getMessage());
		}
		return false;
	}

	/**
	 * 鼠标右键点击
	 */
	public boolean mouseRightClick(){
		try {
			int  i = active.invoke("rightClick").getInt();
			if( i == 1){
				return true;
			}
		} catch (Exception e) {
			log.debug(this,"mouseRightClick","鼠标点击异常捕获:"+e.getMessage());
		}
		return false;
	}
	
}
